package com.laiketui.plugin.common.service;

import com.laiketui.plugin.common.domain.base.DictionaryListModel;
import com.laiketui.plugin.common.domain.base.DictionaryNameModel;
import com.laiketui.plugin.common.domain.lktconst.ErrorCode;
import com.laiketui.plugin.common.domain.vo.DicVo;
import com.laiketui.plugin.common.interfaces.PublicDictionaryService;
import com.laiketui.plugin.common.mapper.DictionaryListModelMapper;
import com.laiketui.plugin.common.mapper.DictionaryNameModelMapper;
import com.laiketui.plugin.common.mapper.ProductClassModelMapper;
import com.laiketui.core.cache.RedisUtil;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.DictionaryConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典操作
 *
 * @author Trick
 * @date 2020/12/28 14:55
 */
@Service
public class PublicDictionaryServiceImpl implements PublicDictionaryService {
    private final Logger logger = LoggerFactory.getLogger(PublicDictionaryServiceImpl.class);

    @Override
    public Map<String, Object> getDictionaryByName(String name) throws LaiKeAPIException {
        Map<String, Object> resultMap;
        try {
            DicVo dicVo = new DicVo();
            dicVo.setName(name);
            resultMap = getDictionaryByName(dicVo);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("获取字典数据 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getDictionaryByName");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getDictionaryByName(DicVo dicVo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            DictionaryNameModel dictionaryNameModel = new DictionaryNameModel();
            dictionaryNameModel.setName(dicVo.getName());
            if (dicVo.getId() != null) {
                //根据id查找字典
                dictionaryNameModel.setId(dicVo.getId());
            }
            if (!dicVo.isShowFather()) {
                dictionaryNameModel.setStatus(DictionaryNameModel.STATUS_OPEN);
                dictionaryNameModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
            }
            dictionaryNameModel = dictionaryNameModelMapper.selectOne(dictionaryNameModel);
            resultMap.put("name", dictionaryNameModel);
            if (dictionaryNameModel != null) {
                //获取明细
                DictionaryListModel dictionaryListModel = new DictionaryListModel();
                dictionaryListModel.setSid(dictionaryNameModel.getId());
                if (!dicVo.isShowChild()) {
                    dictionaryListModel.setStatus(DictionaryNameModel.STATUS_OPEN);
                    dictionaryListModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
                }
                if (dicVo.getSid() != null) {
                    //根据id查找明细
                    dictionaryListModel.setId(dicVo.getSid());
                }
                if (dicVo.getValue() != null) {
                    dictionaryListModel.setValue(dicVo.getValue());
                }
                List<DictionaryListModel> dictionaryListModelList = dictionaryListModelMapper.select(dictionaryListModel);
                resultMap.put("value", dictionaryListModelList);
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("获取字典数据 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getDictionaryByName");
        }
        return resultMap;
    }

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private DictionaryNameModelMapper dictionaryNameModelMapper;

    @Autowired
    private DictionaryListModelMapper dictionaryListModelMapper;

    @Autowired
    private ProductClassModelMapper productClassModelMapper;
}

